<template>
  <div class="indexVideo">
    <div class="player">
      <video
        ref="videoRef"
        :src="videoUrl"
        :poster="videoPoster"
        width="1920"
        height="1080"
        @timeupdate="updateProgress"
        @ended="onVideoEnd"
        @click="togglePlay"
        loop
      ></video>
      
      <!-- 中央播放按钮 -->
      <div 
        class="center-play-button" 
        :class="{ hidden: isPlaying, playing: !isPlaying }"
        @click="togglePlay"
      ></div>

      <div class="controls" @click.stop>
        <!-- Controls bar -->
        <div class="controls-bar">
          <!-- Left controls -->
          <div class="left-controls">
            <button class="control-btn" @click="togglePlay">
              <span v-if="isPlaying">❚❚</span>
              <span v-else>▶</span>
            </button>
            <span class="time">{{ formatTime(currentTime) }} / {{ formatTime(duration) }}</span>
          </div>
          <div class="progress-container" @click="seekVideo">
            <div class="progress-background"></div>
            <div
              class="progress-filled"
              :style="{ width: progress + '%' }"
            ></div>
            <div
              class="progress-thumb"
              :style="{ left: progress + '%' }"
              @mousedown="startDrag"
            ></div>
          </div>
          <!-- Right controls -->
          <div class="right-controls">
            <button class="control-btn" @click="toggleMute">
              <span v-if="isMuted">🔇</span>
              <span v-else>🔊</span>
            </button>
            <button class="control-btn" @click="toggleFullscreen">
              ⛶
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from "vue";

const videoUrl = "/static/che.mp4";
const videoPoster = "/static/poster.webp";
const videoRef = ref(null);
const isPlaying = ref(false);
const isMuted = ref(true);
const currentTime = ref(0);
const duration = ref(0);
const progress = ref(0);
const isDragging = ref(false);

// 存储事件监听器函数引用
const updateProgressHandler = () => updateProgress();
const playHandler = () => isPlaying.value = true;
const pauseHandler = () => isPlaying.value = false;

// Format time to MM:SS
const formatTime = (time) => {
  if (!time || isNaN(time)) return "0:00";
  const minutes = Math.floor(time / 60);
  const seconds = Math.floor(time % 60);
  return `${minutes}:${seconds < 10 ? "0" : ""}${seconds}`;
};

// Toggle play/pause
const togglePlay = () => {
  if (videoRef.value.paused) {
    videoRef.value.play();
    isPlaying.value = true;
  } else {
    videoRef.value.pause();
    isPlaying.value = false;
  }
};

// Toggle mute
const toggleMute = () => {
  videoRef.value.muted = !videoRef.value.muted;
  isMuted.value = videoRef.value.muted;
};

// Toggle fullscreen
const toggleFullscreen = () => {
  if (!document.fullscreenElement) {
    document.querySelector('.indexVideo').requestFullscreen().catch(err => {
      console.error(`全屏请求错误: ${err.message}`);
    });
  } else {
    if (document.exitFullscreen) {
      document.exitFullscreen();
    }
  }
};

// Update progress bar
const updateProgress = () => {
  if (!isDragging.value && videoRef.value) {
    currentTime.value = videoRef.value.currentTime;
    duration.value = videoRef.value.duration || 0;
    progress.value = duration.value ? (currentTime.value / duration.value) * 100 : 0;
  }
};

// Seek video when clicking progress bar
const seekVideo = (e) => {
  const progressContainer = e.currentTarget;
  const rect = progressContainer.getBoundingClientRect();
  const clickPosition = e.clientX - rect.left;
  const progressWidth = rect.width;
  const seekPercentage = (clickPosition / progressWidth) * 100;

  progress.value = Math.min(100, Math.max(0, seekPercentage));
  if (videoRef.value && duration.value) {
    videoRef.value.currentTime = (progress.value / 100) * duration.value;
  }
};

// Handle video end
const onVideoEnd = () => {
  isPlaying.value = false;
  progress.value = 100;
};

// Drag functionality for progress thumb
const startDrag = (e) => {
  isDragging.value = true;
  document.addEventListener("mousemove", onDrag);
  document.addEventListener("mouseup", stopDrag);
  e.preventDefault();
};

const onDrag = (e) => {
  if (!isDragging.value) return;

  const progressContainer = document.querySelector(".progress-container");
  const rect = progressContainer.getBoundingClientRect();
  const progressWidth = rect.width;
  let dragPosition = e.clientX - rect.left;

  dragPosition = Math.max(0, Math.min(progressWidth, dragPosition));
  progress.value = (dragPosition / progressWidth) * 100;
  if (videoRef.value && duration.value) {
    videoRef.value.currentTime = (progress.value / 100) * duration.value;
  }
};

const stopDrag = () => {
  isDragging.value = false;
  document.removeEventListener("mousemove", onDrag);
  document.removeEventListener("mouseup", stopDrag);
};

// Initialize video
onMounted(() => {
  if (videoRef.value) {
    videoRef.value.muted = true;
    videoRef.value.autoplay = false;
    videoRef.value.addEventListener("play", playHandler);
    videoRef.value.addEventListener("pause", pauseHandler);
    videoRef.value.addEventListener("timeupdate", updateProgressHandler);
    videoRef.value.addEventListener("ended", onVideoEnd);
  }
});

// Clean up event listeners
onUnmounted(() => {
  if (videoRef.value) {
    videoRef.value.removeEventListener("play", playHandler);
    videoRef.value.removeEventListener("pause", pauseHandler);
    videoRef.value.removeEventListener("timeupdate", updateProgressHandler);
    videoRef.value.removeEventListener("ended", onVideoEnd);
    videoRef.value.pause();
    videoRef.value = null;
  }
  
  // Clean up drag event listeners
  document.removeEventListener("mousemove", onDrag);
  document.removeEventListener("mouseup", stopDrag);
});
</script>

<style lang="less" scoped>
.indexVideo {
  max-width: 1920px;
  width: 100%;
  height: 1080px;
  margin: 0 auto;
  background: #1a1b1c;
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.5);
}

.player {
  width: 100%;
  height: 100%;
  position: relative;

  video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    cursor: pointer;
  }
  
  .title {
    position: absolute;
    top: 30px;
    left: 30px;
    color: white;
    font-size: 28px;
    font-weight: 600;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
    z-index: 2;
  }
}

/* 中央播放按钮 */
.center-play-button {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 160px;
  height: 160px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 
    0 0 0 1px rgba(255, 255, 255, 0.1),
    0 10px 20px rgba(0, 0, 0, 0.19),
    0 6px 6px rgba(0, 0, 0, 0.23),
    inset 0 0 20px rgba(255, 255, 255, 0.05);
  z-index: 5;
  opacity: 1;
}

.center-play-button.hidden {
  opacity: 0;
  pointer-events: none;
}

.center-play-button:hover {
  background: rgba(255, 255, 255, 0.15);
  transform: translate(-50%, -50%) scale(1.05);
  box-shadow: 
    0 0 0 1px rgba(255, 255, 255, 0.15),
    0 15px 30px rgba(0, 0, 0, 0.25),
    0 10px 10px rgba(0, 0, 0, 0.22),
    inset 0 0 25px rgba(255, 255, 255, 0.08);
}

.center-play-button:active {
  transform: translate(-50%, -50%) scale(0.95);
}

.center-play-button::before {
  content: '';
  width: 0;
  height: 0;
  border-top: 25px solid transparent;
  border-bottom: 25px solid transparent;
  border-left: 50px solid rgba(255, 255, 255, 0.9);
  margin-left: 15px;
  filter: drop-shadow(0 0 5px rgba(0, 0, 0, 0.5));
}


.controls {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 10;
}

.progress-container {
  height: 6px;
  width: 100%;
  position: relative;
  cursor: pointer;

  .progress-background {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 100%;
    background: rgba(0, 0, 0, 0.1);
    border-radius: 3px;
  }

  .progress-filled {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 3px;
  }

  .progress-thumb {
    position: absolute;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 6px;
    height: 30px;
    background: #fff;
    border-radius: 3px;
    cursor: pointer;
  }
}

.controls-bar {
  height: 88px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  background: rgba(0, 0, 0, 0.6);
  box-shadow: rgba(50, 50, 93, 0.25) 0px 30px 60px -12px inset, 
              rgba(0, 0, 0, 0.3) 0px 18px 36px -18px inset;
  backdrop-filter: blur(3px);
  gap: 40px;

  .left-controls,
  .right-controls {
    display: flex;
    align-items: center;
    gap: 15px;
  }

  .control-btn {
    background: transparent;
    border: none;
    color: white;
    font-size: 30px;
    cursor: pointer;
    padding: 5px;
    transition: transform 0.2s;
    
    &:hover {
      transform: scale(1.1);
    }
  }

  .time {
    color: white;
    font-family: Arial, sans-serif;
    font-size: 20px;
    min-width: 120px;
  }
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .indexVideo {
    height: 800px;
  }
}

@media (max-width: 768px) {
  .indexVideo {
    height: 500px;
    border-radius: 8px;
  }
  
  .center-play-button {
    width: 80px;
    height: 80px;
  }
  
  .controls-bar {
    height: 70px;
    padding: 0 15px;
    gap: 20px;
    
    .control-btn {
      font-size: 24px;
    }
    
    .time {
      font-size: 16px;
      min-width: 100px;
    }
  }
  
  .title {
    font-size: 20px !important;
    top: 20px !important;
    left: 20px !important;
  }
}

@media (max-width: 480px) {
  .indexVideo {
    height: 400px;
  }
  
  .center-play-button {
    width: 60px;
    height: 60px;
  }
  
  .center-play-button::before {
    border-top-width: 12px;
    border-bottom-width: 12px;
    border-left-width: 20px;
  }
  
  .controls-bar {
    height: 60px;
    padding: 0 10px;
    gap: 15px;
    
    .control-btn {
      font-size: 20px;
    }
    
    .time {
      font-size: 14px;
      min-width: 90px;
    }
  }
  
  .title {
    font-size: 18px !important;
    top: 15px !important;
    left: 15px !important;
  }
}
</style>